const models = require('./db')
const express = require('express')
const router = express.Router()
const mysql = require('mysql')
const $sql = require('./splMap')
const {
	json
} = require('body-parser')

// 连接数据库
const conn = mysql.createConnection(models.mysql)
conn.connect()
const jsonWrite = function (res, ret) {
	if (typeof ret === 'undefined') {
		res.json({
			code: '1',
			msg: '操作成功'
		})
	} else {
		console.log('success！！！');
		res.json(ret)
	}
}
// --------------------黑马优购接口---------------------------
//获取首页轮番图数据
router.get('/getHomeBanners', (req, res) => {
	const sql = $sql.getHomeBanners
	const params = req.body
	console.log('添加', params, sql)
	conn.query(sql, function (err, result) {
		if (err) {
			console.log(err)
		}
		if (result) {
			jsonWrite(res, result)
		}
	})
})
//获取首页时尚女装
router.get('/getHomegoodsgirl', (req, res) => {
	const sql = $sql.getHomegoodsgirl
	const params = req.body
	console.log('添加', params, sql)
	conn.query(sql, function (err, result) {
		if (err) {
			console.log(err)
		}
		if (result) {
			jsonWrite(res, result)
		}
	})
})
//获取首页户外运动
router.get('/getHomegoodssport', (req, res) => {
	const sql = $sql.getHomegoodssport
	const params = req.body
	console.log('添加', params, sql)
	conn.query(sql, function (err, result) {
		if (err) {
			console.log(err)
		}
		if (result) {
			jsonWrite(res, result)
		}
	})
})
//获取首页箱包配饰
router.get('/getHomegoodsgift', (req, res) => {
	const sql = $sql.getHomegoodsgift
	const params = req.body
	console.log('添加', params, sql)
	conn.query(sql, function (err, result) {
		if (err) {
			console.log(err)
		}
		if (result) {
			jsonWrite(res, result)
		}
	})
})

// 根据id进行查找商品详情
router.post('/getGoodsDetail', (req, res) => {
	const sql = $sql.getGoodsDetail
	const params = req.body
	console.log('添加', params, sql)
	conn.query(sql, [params.goods_id], function (err, result) {
		if (err) {
			console.log(err)
		}
		if (result) {
			console.log('操作成功')
			let data = result[0]
			jsonWrite(res, data)
		}
	})
})

// 增加订单列表
router.post('/addOrderList', (req, res) => {
	const sql = $sql.addOrderList
	const params = req.body
	console.log('添加', params, sql)
	conn.query(sql, [params.goods_id, params.goods_small_logo, params.goods_name, params.goods_count, params
		.goods_price,
	params.goods_address, params.goods_if
	], function (err, result) {
		if (err) {
			console.log(err)
		}
		if (result) {
			console.log('操作成功')
			let data = result[0]
			jsonWrite(res, data)
		}
	})
})

// 根据订单状态返回订单列表
router.post('/getOrderListChoose', (req, res) => {
	const sql = $sql.getOrderListChoose
	const params = req.body
	console.log('添加', params, sql)
	conn.query(sql, [params.goods_state], function (err, result) {
		if (err) {
			console.log(err)
		}
		if (result) {
			console.log('操作成功', result)
			jsonWrite(res, result)
		}
	})
})

// 拿整个订单列表
router.get('/getOrderList', (req, res) => {
	const sql = $sql.getOrderList
	const params = req.body
	console.log('添加', params, sql)
	conn.query(sql, function (err, result) {
		if (err) {
			console.log(err)
		}
		if (result) {
			jsonWrite(res, result)
		}
	})
})

// 根据关键字查询商品列表
router.post('/searchgoods', (req, res) => {
	const sql = $sql.searchgoods
	const params = req.body
	console.log('添加', params, sql)
	var t = '%' + params.goods_name + '%'
	conn.query(sql, [t, t, t], function (err, result) {
		if (err) {
			console.log(err)
		}
		if (result) {
			console.log('操作成功', result)
			jsonWrite(res, result)
		}
	})
})

// 获取所有商品数据
router.get('/showallgoods', (req, res) => {
	const sql = $sql.showallgoods
	const params = req.body
	console.log('添加', params, sql)
	conn.query(sql, function (err, result) {
		if (err) {
			console.log(err)
		}
		if (result) {
			jsonWrite(res, result)
		}
	})
})

// 编辑商品详情
router.post('/changegoodsdetail', (req, res) => {
	const sql = $sql.changegoodsdetail
	const params = req.body
	console.log('添加', params, sql)
	conn.query(sql, [params.goods_name, params.goods_price, params.goods_kucun, params.goods_weight, params.goods_small_logo, params.goods_type, params.goods_pics, params.goods_introduce, params.goods_id], function (err, result) {
		if (err) {
			console.log(err)
		}
		if (result) {
			console.log('操作成功', result)
			jsonWrite(res, result)
		}
	})
})

// 删除商品详情
router.post('/deletegoodsdetail', (req, res) => {
	const sql = $sql.deletegoodsdetail
	const params = req.body
	console.log('添加', params, sql)
	conn.query(sql, [params.goods_id], function (err, result) {
		if (err) {
			console.log(err)
		}
		if (result) {
			console.log('操作成功', result)
			jsonWrite(res, result)
		}
	})
})

// 增加商品详情
router.post('/addgoodsdetail', (req, res) => {
	const sql = $sql.addgoodsdetail
	const params = req.body
	console.log('添加', params, sql)
	conn.query(sql, [params.goods_id, params.goods_name, params.goods_price, params.goods_kucun, params.goods_weight, params.goods_small_logo, params.goods_type, params.goods_pics, params.goods_introduce], function (err, result) {
		if (err) {
			console.log(err)
		}
		if (result) {
			console.log('操作成功', result)
			jsonWrite(res, result)
		}
	})
})

// 简单增加商品
router.post('/addgoodsdetail', (req, res) => {
	const sql = $sql.addgoodsdetail
	const params = req.body
	console.log('添加', params, sql)
	conn.query(sql, [params.goods_id, params.goods_name, params.goods_price, params.goods_small_logo], function (err, result) {
		if (err) {
			console.log(err)
		}
		if (result) {
			console.log('操作成功', result)
			jsonWrite(res, result)
		}
	})
})

// 过滤商品，选择类型商品
router.post('/filtergoodsdetail', (req, res) => {
	const sql = $sql.filtergoodsdetail
	const params = req.body
	console.log('添加', params, sql)
	conn.query(sql, [params.goods_type], function (err, result) {
		if (err) {
			console.log(err)
		}
		if (result) {
			console.log('操作成功', result)
			jsonWrite(res, result)
		}
	})
})

// 搜索商品
router.post('/searchgoodsdetail', (req, res) => {
	const sql = $sql.searchgoodsdetail
	const params = req.body
	var t = '%' + params.goods_name + '%'
	console.log('添加', params, sql)
	conn.query(sql, [t], function (err, result) {
		if (err) {
			console.log(err)
		}
		if (result) {
			console.log('操作成功', result)
			jsonWrite(res, result)
		}
	})
})

// 搜索商品id
router.post('/searchorderid', (req, res) => {
	const sql = $sql.searchorderid
	const params = req.body
	console.log('添加', params, sql)
	conn.query(sql, [params.goods_id], function (err, result) {
		if (err) {
			console.log(err)
		}
		if (result) {
			console.log('操作成功', result)
			jsonWrite(res, result)
		}
	})
})

// 删除订单id
router.post('/deleteorderList', (req, res) => {
	const sql = $sql.deleteorderList
	const params = req.body
	console.log('添加', params, sql)
	conn.query(sql, [params.goods_id], function (err, result) {
		if (err) {
			console.log(err)
		}
		if (result) {
			console.log('操作成功', result)
			jsonWrite(res, result)
		}
	})
})

// 更改订单总金额，地址，发货状态
router.post('/updateordrtListmessage', (req, res) => {
	const sql = $sql.updateordrtListmessage
	const params = req.body
	console.log('添加', params, sql)
	conn.query(sql, [params.goods_state, params.goods_all, params.goods_address, params.goods_if, params.goods_id], function (err, result) {
		if (err) {
			console.log(err)
		}
		if (result) {
			console.log('操作成功', result)
			jsonWrite(res, result)
		}
	})
})

// 进行更改订单状态-退货退款
router.post('/updateordrtListstate', (req, res) => {
	const sql = $sql.updateordrtListstate
	const params = req.body
	console.log('添加', params, sql)
	conn.query(sql, [params.goods_state, params.goods_id], function (err, result) {
		if (err) {
			console.log(err)
		}
		if (result) {
			console.log('操作成功', result)
			jsonWrite(res, result)
		}
	})
})





module.exports = router